package cn.edu.pku.ss.situationperceiver;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

public class RSHandler extends DefaultHandler {
	
	private int id;
	private Situation situation;
	private Relation relation;
	private HashMap<Integer,Situation> situations;
	private List<Relation> relations;
	private List<Relation> incompatibles;
	
	private final int ID = 1;
	private final int NAME = 2;
	private final int TRANS = 3;
	private final int RELATION = 4;
	private final int INCOMP = 5;
	private int currentState = 0;
	
	public RSHandler()
	{
		situations = new HashMap<Integer,Situation>();
		relations = new ArrayList<Relation>();
		incompatibles = new ArrayList<Relation>();
	}
	
	public HashMap<Integer,Situation> getSituations()
	{
		return situations;
	}
	
	public List<Relation> getRelations()
	{
		return relations;
	}
	
	public List<Relation> getIncompatibles()
	{
		return incompatibles;
	}
	
	@Override
	public void startElement(String uri, String localName, String qName,
			Attributes attributes) throws SAXException {
		// TODO Auto-generated method stub
		if(localName.equals("situation"))
		{
			situation = new Situation();
			return;
		}
		if(localName.equals("id"))
		{
			currentState = ID;
			return;
		}
		if(localName.equals("name"))
		{
			currentState = NAME;
			return;
		}
		if(localName.equals("transparent"))
		{
			currentState = TRANS;
			return;
		}
		if(localName.equals("relation"))
		{
			relation = new Relation();
			currentState = RELATION;
			return;
		}
		if(localName.equals("incompatible"))
		{
			relation = new Relation();
			currentState = INCOMP;
			return;
		}
	}
	
	@Override
	public void characters(char[] ch, int start, int length)
			throws SAXException {
		// TODO Auto-generated method stub
		String content = new String(ch, start, length);
		switch (currentState) {
		case ID:
			id = Integer.parseInt(content);
			currentState = 0;
			break;
		case NAME:
			situation.setName(content);
			currentState = 0;
			break;
		case TRANS:
			if(content.equals("true"))
				situation.setTransparent(true);
			else
				situation.setTransparent(false);
			currentState = 0;
			break;
		case RELATION:
			String[] items = content.split(",");
			relation.setPrevious(Integer.parseInt(items[0]));
			relation.setNext(Integer.parseInt(items[1]));
			currentState = 0;
			break;
		case INCOMP:
			String[] item = content.split(",");
			relation.setPrevious(Integer.parseInt(item[0]));
			relation.setNext(Integer.parseInt(item[1]));
			currentState = 0;
			break;
		default:
			break;
		}
	}
	
	@Override
	public void endElement(String uri, String localName, String qName)
			throws SAXException {
		// TODO Auto-generated method stub
		if(localName.equals("situation"))
		{
			situations.put(id, situation);
			return;
		}
		if(localName.equals("relation"))
		{
			relations.add(relation);
			return;
		}
		if(localName.equals("incompatible"))
		{
			incompatibles.add(relation);
			return;
		}
	}
}
